import NeDB from '@seald-io/nedb';
import path from 'path'
import { app } from 'electron'

class DBUtils {
	public configdb:NeDB
	constructor(){
		
		this.configdb=new NeDB({
			filename: path.join(app.getPath('userData'), '/data/db/config.db'),
			timestampData: true,
			autoload: true,
		})
	}
	
	public async setLocalName(name:string){
		let nameConfig=await this.configdb.findOneAsync({type:"name"})
		if(nameConfig){
			await this.configdb.updateAsync({_id:nameConfig._id},{$set:{name}})
		}else{
			await this.configdb.insertAsync({type:"name",name})
			let list=this.configdb.findAsync({})
		}
	}

	public async getLocalName(){
		let nameConfig=await this.configdb.findOneAsync({type:"name"})
		if(nameConfig){
			return nameConfig.name
		}else{
			return ""
		}
	}
}

export default new DBUtils()